package com.dyw.springbootpersonal.controller;

import com.dyw.springbootpersonal.entity.User;
import com.dyw.springbootpersonal.result.Result;
import com.dyw.springbootpersonal.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Devil
 * @create 2022-01-29-21:03
 */
@RestController
@RequestMapping("/users")
public class MyController {

    @Autowired
    private UserServiceImpl userService;

    @GetMapping
    public Result getAll() {
        return new Result(userService.selectUsers());
    }

    @PostMapping
    public Result add(@RequestBody User user) {
        return new Result(userService.insertUser(user));
    }

    @DeleteMapping("/{id}")
    public Result remove(@PathVariable Integer id) {
        return new Result(userService.deleteUserById(id));
    }

    @PutMapping
    public Result update(@RequestBody User user) {
        return new Result(userService.updateById(user));
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        User user = userService.selectById(id);
        if (user == null) {
            return new Result(false, null);
        } else {
            return new Result(user);
        }
    }
}
